package com.crm.service.impl;

import com.crm.mapper.MenuMapper;
import com.crm.pojo.vo.MenuVo;
import com.crm.pojo.vo.TreeVo;
import com.crm.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    //菜单权限树状查询 todo 菜单权限子菜单查询
    @Override
    public List<TreeVo> treeselect(long id) {
        List<TreeVo> treeVos = new ArrayList<>();
        List<MenuVo> menuVos = menuMapper.children(id);
        if (menuVos!=null && menuVos.size()>0){
            treeVos = treeMenu(id,menuVos);
        }
        return treeVos;
    }

    //递归查找菜单树
    private List<TreeVo> treeMenu(Long id, List<MenuVo> menuVos) {
        ArrayList<TreeVo> treeVos = new ArrayList<>();
        for (MenuVo menuVo : menuVos) {
            TreeVo treeVo = new TreeVo();
            treeVo.setId(menuVo.getMenuId());
            treeVo.setLabel(menuVo.getMenuName());
            List<MenuVo> childens = menuMapper.children(menuVo.getMenuId());
            if (childens.size()>0){
                treeVo.setChildren(treeMenu(menuVo.getMenuId(),childens));
            }
            treeVos.add(treeVo);
        }
        return treeVos;
    }
}
